package com.unitechs.trap.excel.webapi.service.impl;

import com.unitechs.trap.common.pojo.trap.entity.AllData;
import com.unitechs.trap.excel.service.IReadAllDataService;
import com.unitechs.trap.excel.webapi.mapper.CoInfoMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@DubboService
@Service
@Slf4j
public class ReadAllDataServiceImpl implements IReadAllDataService {

    @Autowired
    private CoInfoMapper mapper;

    /**
     * 从Excel中读取数据
     * 并赋值部分数据
     *
     * @param url
     * @param coName
     * @return 返回alldata的集合
     */
    private List<AllData> readAllData_A(String url, String coName, String version) {


        List<AllData> list = new ArrayList<>();
        AllData lastData = new AllData();
        try {
            File file = new File(url);
            File[] files = file.listFiles();
            for (File f : files) {
                if (!f.isDirectory() && f.getName().endsWith(".xls")) {
                    log.info("查询了"+f.getName()+"文件");

                    //创建工作簿对象
                    HSSFWorkbook xssfWorkbook = new HSSFWorkbook(new FileInputStream(f.getPath()));
                    //读取第1个工作表
                    HSSFSheet sheet = xssfWorkbook.getSheetAt(0);
                    //获取最后一行的num，即总行数。此处从0开始
                    int maxRow = sheet.getLastRowNum();
                    for (int row = 1; row <= maxRow; row++) {
                        AllData allData = new AllData();

                        allData.setTrapOid(lastData, sheet, row, version);
                        if (allData.getTrapOid().replaceAll(" ", "").matches("(\\.\\d+)+")){
                            allData.setTrapOid(allData.getTrapOid().replaceAll(" ", ""));
                        }else if (("."+allData.getTrapOid().replaceAll(" ", "")).matches("(\\.\\d+)+")){
                            allData.setTrapOid("."+allData.getTrapOid().replaceAll(" ", ""));
                        }else {
                            continue;
                        }
                        allData.setTrapTypeCode(lastData, sheet, row, version);
                        allData.setTrapTypeCodeId(lastData, sheet, row, version);
                        String coChinaName = mapper.getCoDescrByCoName(coName);
                        allData.setTrapTypeDesc(allData, lastData, sheet, row, coChinaName, version);
                        allData.setAlarmlevel(lastData, sheet, row, version);
                        allData.setAlarmclassCode(lastData, sheet, row, version);
                        allData.setAlarmtypeDescr(lastData, sheet, row, coName, version);
                        lastData = allData;
                        list.add(allData);
                    }
                }
            }

        } catch (IOException e) {
            e.printStackTrace();
        }

        return list;
    }

    /**
     * B版本
     *
     * @param url
     * @param coName
     * @param version
     * @return
     */

    private List<AllData> readAllData_B(String url, String coName, String version) {
        List<AllData> list = new ArrayList<>();
        AllData lastData = new AllData();
        try {

            File file = new File(url);
            File[] files = file.listFiles();
            for (File f : files) {
                if (!f.isDirectory() && f.getName().endsWith(".xls")) {


                    //创建工作簿对象
                    HSSFWorkbook xssfWorkbook = new HSSFWorkbook(new FileInputStream(f.getPath()));
                    //读取第1个工作表
                    HSSFSheet sheet = xssfWorkbook.getSheetAt(0);
                    //获取最后一行的num，即总行数。此处从0开始
                    int maxRow = sheet.getLastRowNum();
                    for (int row = 1; row <= maxRow; row++) {
                        AllData allData = new AllData();

                        allData.setTrapOid(lastData, sheet, row, version);
                        allData.setTrapTypeCode(lastData, sheet, row, coName, version);
                        allData.setTrapTypeCodeId(lastData, sheet, row, version);
                        String coChinaName = mapper.getCoDescrByCoName(coName);
                        allData.setTrapTypeDesc(allData, lastData, sheet, row, coChinaName, version);
                        allData.setAlarmlevel(lastData, sheet, row, version);
                        allData.setAlarmclassCode(lastData, sheet, row, version);
                        allData.setAlarmtypeDescr(lastData, sheet, row, coName, version);
                        lastData = allData;
                        list.add(allData);
                    }
                }
            }


        } catch (IOException e) {
            e.printStackTrace();
        }

        return list;
    }

    /**
     * C版本
     *
     * @param url
     * @param coName
     * @param version
     * @return
     */

    private List<AllData> readAllData_C(String url, String coName, String version) {


        List<AllData> list = new ArrayList<>();
        AllData lastData = new AllData();
        try {
            File file = new File(url);
            File[] files = file.listFiles();
            for (File f : files) {
                if (!f.isDirectory() && f.getName().endsWith(".xls")) {

                    //创建工作簿对象
                    HSSFWorkbook xssfWorkbook = new HSSFWorkbook(new FileInputStream(f.getPath()));
                    //读取第1个工作表
                    HSSFSheet sheet = xssfWorkbook.getSheetAt(0);
                    //获取最后一行的num，即总行数。此处从0开始
                    int maxRow = sheet.getLastRowNum();
                    for (int row = 1; row <= maxRow; row++) {
                        AllData allData = new AllData();

                        allData.setTrapOid(lastData, sheet, row, version);
                        allData.setTrapTypeCode(lastData, sheet, row, coName, version);
                        allData.setTrapTypeCodeId(lastData, sheet, row, version);
                        String coChinaName = mapper.getCoDescrByCoName(coName);
                        allData.setTrapTypeDesc(allData, lastData, sheet, row, coChinaName, version);
                        allData.setAlarmlevel(lastData, sheet, row, version);
                        allData.setAlarmclassCode(lastData, sheet, row, version);
                        allData.setAlarmtypeDescr(lastData, sheet, row, coName, version);
                        lastData = allData;
                        list.add(allData);
                    }
                }
            }


        } catch (IOException e) {
            e.printStackTrace();
        }

        return list;
    }

    /**
     * D版本
     *
     * @param url
     * @param coName
     * @param version
     * @return
     */

    private List<AllData> readAllData_D(String url, String coName, String version) {


        List<AllData> list = new ArrayList<>();
        AllData lastData = new AllData();
        try {
            File file = new File(url);
            File[] files = file.listFiles();
            for (File f : files) {
                if (!f.isDirectory() && f.getName().endsWith(".xls")) {

                    //创建工作簿对象
                    HSSFWorkbook xssfWorkbook = new HSSFWorkbook(new FileInputStream(f.getPath()));
                    //读取第1个工作表
                    HSSFSheet sheet = xssfWorkbook.getSheetAt(0);
                    //获取最后一行的num，即总行数。此处从0开始
                    int maxRow = sheet.getLastRowNum();
                    for (int row = 1; row <= maxRow; row++) {
                        AllData allData = new AllData();

                        allData.setTrapOid(lastData, sheet, row, version);
                        allData.setTrapTypeCode(lastData, sheet, row, coName, version);
                        allData.setTrapTypeCodeId(lastData, sheet, row, version);
                        String coChinaName = mapper.getCoDescrByCoName(coName);
                        allData.setTrapTypeDesc(allData, lastData, sheet, row, coChinaName, version);
                        allData.setAlarmlevel(lastData, sheet, row, version);
                        allData.setAlarmclassCode(lastData, sheet, row, version);
                        allData.setAlarmtypeDescr(lastData, sheet, row, coName, version);
                        if (sheet.getRow(row).getCell(11)==null||sheet.getRow(row).getCell(11).toString().isEmpty()){
                            allData.setUpDown(lastData.getUpDown());
                        }else {
                            allData.setUpDown(sheet.getRow(row).getCell(11).toString());
                        }
                        log.info("AllData数据{}",allData);
                        lastData = allData;
                        list.add(allData);
                    }
                }
            }


        } catch (IOException e) {
            e.printStackTrace();
        }

        return list;
    }


    @Override
    public List<AllData> readAllDataByVersion(String url, String coName, String version) {


        List<AllData> list = new ArrayList<>();
        switch (version) {
            case "A":
                list = readAllData_A(url, coName, version);
                break;
            case "B":
                list = readAllData_B(url, coName, version);
                break;
            case "C":
                list = readAllData_C(url, coName, version);
                break;
            case "D":
                list =readAllData_D(url, coName, version);
        }
        return list;
    }
}
